﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using FinanceCalculators;
using System.IO.IsolatedStorage;
using System.Xml.Linq;
using System.IO;

namespace HomeBuyerTools.Views.CompareLoansByRate
{
    public partial class result1 : PhoneApplicationPage
    {
        public result1()
        {
            InitializeComponent();
        }
       

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            string mp = "";
            string tm = "";
            string ir = "";
            string cc = "";
            NavigationContext.QueryString.TryGetValue("mp", out mp);
            NavigationContext.QueryString.TryGetValue("tm", out tm);
            NavigationContext.QueryString.TryGetValue("ir", out ir);
            NavigationContext.QueryString.TryGetValue("cc", out cc);
            double mprinciple = Convert.ToDouble(mp);
            double NoOfMonths = Convert.ToDouble(tm) * 12;
            double interestrate = Convert.ToDouble(ir);
            double startInterestRate = 0.0;
            double interestRatio = 0.0;
            switch (cc)
            {
                case "1/8%":
                    interestRatio = 0.125;
                    break;
                case "1/4%":
                    interestRatio = 0.25;
                    break;
                case "3/8%":
                    interestRatio = 3 / 8;
                    break;
                case "1/2%":
                    interestRatio = 1 / 2;
                    break;
                case "5/8%":
                    interestRatio = 5 / 8;
                    break;
                case "3/4%":
                    interestRatio = 3 / 4;
                    break;
                case "7/8%":
                    interestRatio = 7 / 8;
                    break;
                case "1%":
                    interestRatio = 1;
                    break;
                case "select":
                    interestRatio = 0.0;
                    break;
            }
            startInterestRate = Math.Floor(interestrate);
            List<double> RateList = new List<double>();
            for (int i = 1; i <= 5; i++)
            {
                RateList.Add(startInterestRate);
                startInterestRate = startInterestRate + interestRatio;
            }

            List<CompareLoanByRate> objRateList = new List<CompareLoanByRate>();

            foreach (double indRate in RateList)
            {
                CompareLoanByRate objRate = new CompareLoanByRate();
                double totalinterest = 0.0;
                double MonthlyPayment = 0.0;
                double Interest = Math.Round(mprinciple * (indRate / 100 / 12), 2);
                double payment = Math.Round((mprinciple * (indRate / 100 / 12)) / (1 - (Math.Pow(1 / (1 + (indRate / 100 / 12)), NoOfMonths))), 2);
                double principle = Math.Round(payment - Interest, 2);
                double balance = Math.Round(mprinciple - principle, 2);
                MonthlyPayment = payment;

                totalinterest = Interest;
                for (int i = 1; i <= NoOfMonths; i++)
                {
                    Interest = Math.Round(balance * (indRate / 100 / 12), 2);
                    payment = Math.Round((balance * (indRate / 100 / 12)) / (1 - (Math.Pow(1 / (1 + (indRate / 100 / 12)), (NoOfMonths - i)))), 2);
                    principle = Math.Round(payment - Interest, 2);
                    balance = Math.Round(balance - principle, 2);
                    totalinterest =totalinterest + Interest;
                    totalinterest = Math.Round(totalinterest, 2);

                    
                }



                objRate.InterestRate = indRate.ToString();
                objRate.MonthlyPayment = MonthlyPayment.ToString();
                objRate.TotalInterest = totalinterest.ToString();
                objRate.TotalPrinciple = mprinciple.ToString();
                objRate.TotalPayments = (mprinciple + totalinterest).ToString();

                objRateList.Add(objRate);
            }
            lbxComLoanByRate.ItemsSource = objRateList;
        }

        private void ApplicationBarIconButton_Click(object sender, EventArgs e)
        {
            NavigationService.GoBack();
        }
    }
}